package com.lzx.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzx.model.FileEntity;
import com.lzx.model.User;
import com.lzx.model.index.GrainSales;
import com.lzx.model.index.GrainYield;
import com.lzx.model.index.PlantArea;
import com.lzx.model.index.PlantType;
import com.lzx.model.zlzs.*;

import com.lzx.service.FileEntityService;
import com.lzx.service.IndexService;
import com.lzx.service.UserService;
import com.lzx.service.zlzs.*;
import com.lzx.utils.JsonResultUtil;
import com.lzx.utils.SessionUtil;
import org.apache.catalina.connector.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/sy/zlzs")
public class ZlzsController {
    @Autowired
    CommodityService commodityService;
    @Autowired
    CompanyService companyService;
    @Autowired
    FertilizerService fertilizerService;
    @Autowired
    DistributionService distributionService;
    @Autowired
    MachiningService machiningService;
    @Autowired
    PhotoService photoService;
    @Autowired
    GerminationService germinationService;
    @Autowired
    UserService userService;

    @Autowired
    private FileEntityService fileBeanService;


    //质量追溯
    @RequestMapping("/zlzs.html")
    public ModelAndView indexzlzs(HttpServletRequest request,String id) {
        String address = (String) SessionUtil.getAttr(request,"address");
        User user1 = null;
        String countyid="";
        try {
            user1 = userService.queryByUserName(address);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(user1 != null){
            countyid = user1.getUserId();
        }else{
            countyid ="46e6c5f9b6df4fafad156074c4c41ca2" ;
        }

        String companyid = "";
        String commodityid = "";

        ModelAndView mv = new ModelAndView("/system/zlzs");

        //查询商品
        try {
            List<Commodity> list = commodityService.selectlistbycountyid(countyid);
            mv.addObject("list", list);
            if(id == null) {
                Commodity commodity = list.get(0);
                commodityid = commodity.getId();
                companyid = commodity.getEnterpriseid();
                mv.addObject("commodity", commodity);
            }else{
                System.out.println(id);

                Commodity commodity1 = commodityService.selectdemobyid(id);
                commodityid = id;
                companyid = commodity1.getEnterpriseid();
                mv.addObject("commodity", commodity1);
            }
            FileEntity fileEntity = new FileEntity();
            fileEntity.setYwId(commodityid);
            fileEntity.setYwType("commoditytp");
            List<FileEntity> fileBeans = fileBeanService.selectdemolist(fileEntity);
            System.out.println(fileBeans);
            mv.addObject("fileBeans", fileBeans);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //查询企业
        try {
            Company company = companyService.selectdemobyid(companyid);
            mv.addObject("company", company);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //肥料使用
        try {
            Fertilizer fertilizer = fertilizerService.selectdemobyid(commodityid);
            mv.addObject("fertilizer", fertilizer);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //配送管理
        try {
            Distribution distribution = distributionService.selectdemobyid(commodityid);
            mv.addObject("distribution", distribution);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //加工管理
        try {
            Machining machining=machiningService.selectdemobyid(commodityid);
            mv.addObject("machining", machining);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //选种催芽
        try {
            Germination germination=germinationService.selectdemobyid(commodityid);
            mv.addObject("germination", germination);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //全生长期图片
        Photo photo=new Photo();
        photo.setPremarks("1");
        photo.setCommodityid(commodityid);
        List ids=new ArrayList();
        List<FileEntity> tps=new ArrayList<>();
        try {
            List<Photo>  list =photoService.selectdemolistyue(photo);
            if(list.size()>0){
                for(Photo po:list){
                    ids.add(po.getId());
                }
            }
            //System.out.println("ids------------"+ids.toString());
            for(int i=0;i<ids.size();i++){
                FileEntity file = new FileEntity();
                file.setYwId((String) ids.get(i));
                file.setYwType("qszqtp");
                List<FileEntity> fileBeans = fileBeanService.selectdemolist(file);
                if(fileBeans.size()>0){
                    for(FileEntity fi:fileBeans){
                        tps.add(fi);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if(SessionUtil.getUser(request).getIsAdmin().equals("Y")){
            mv.addObject("isAdmn","TRUE");
        }else{
            mv.addObject("isAdmn","FALSE");
        }

        mv.addObject("tps", tps);

        return mv;
    }

    //全生长期图片，根据月份切换
    @ResponseBody
    @RequestMapping("/zlzsqszqtp")
    public String lsjg(String commodityid,String yue) {

        Photo photo=new Photo();
        photo.setPremarks(yue);
        photo.setCommodityid(commodityid);
        List ids=new ArrayList();
        List<FileEntity> tps=new ArrayList<>();
        try {
            List<Photo>  list =photoService.selectdemolistyue(photo);
            if(list.size()>0){
                for(Photo po:list){
                    ids.add(po.getId());
                }
            }

            System.out.println("ids------------"+ids.toString());

            for(int i=0;i<ids.size();i++) {
                FileEntity file = new FileEntity();
                file.setYwId((String) ids.get(i));
                file.setYwType("qszqtp");
                List<FileEntity> fileBeans = fileBeanService.selectdemolist(file);
                if(fileBeans.size()>0){
                    for(FileEntity fi:fileBeans){
                        tps.add(fi);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonstr = new JSONObject();
        jsonstr.put("tps", tps);
        return jsonstr.toJSONString();
    }
    @RequestMapping("/zlzsnew.html")
    public ModelAndView zlzsnew(HttpServletRequest request,String id,HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("zhny/zhiLiangZhuiSu");
        String locale = request.getParameter("locale");
//        System.out.println("index========"+locale);
        writeCookie(request,response,"locale",locale);
        User user = SessionUtil.getUser(request);
        mv.addObject("user", user);

        return mv;
    }
    public static void writeCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String value) {
        Cookie cookie = new Cookie(cookieName, value);
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
    }
}
